//
//  SearchViewController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-23.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "SearchViewController.h"
#import "GiftDetailViewController.h"
#import "DataService.h"

@interface SearchViewController ()

@property (nonatomic, strong) NSArray *hotWorlds;

@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"搜索";
        self.navigationItem.titleView = t;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.searchScrollView.contentSize = CGSizeMake(kScreenWidth, self.searchScrollView.height + 45);
    _ddList = [[DDList alloc] initWithStyle:UITableViewStylePlain];
	_ddList._delegate = self;
    [_ddList.view setFrame:CGRectMake(30, -136, 200, 0)];
	[self.view addSubview:_ddList.view];
    
//    设置背景View圆角
    self.backgroundView.layer.cornerRadius = 6;
    self.backgroundView.layer.masksToBounds = YES;
    
    
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version < 7.0) {
        [[_searchBar.subviews objectAtIndex:0]removeFromSuperview];
    }else
    {
        _searchScrollView.contentSize = CGSizeMake(kScreenWidth, self.searchScrollView.height - 50);
    }
    
//    http://m.a.ewoka.com/apply/about_us
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/top_search"];
    __weak SearchViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [self loadDataFinished:result];
                    }];

}

- (void)loadDataFinished:(NSArray *)result
{
    self.rightView.right = -20;
    self.leftView.left = kScreenWidth;
    
    self.hotWorlds = result;
    for (NSString *str in result) {
        for (int i = 0; i < 5; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10, i * 30 + 20, 120, 20);
            [btn setTitle:result[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//            [btn addTarget:self action:@selector(hotWorldClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn addTarget:self action:@selector(hotWorldClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.leftView addSubview:btn];
        }
        
        for (int i = 5; i < 10; i ++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10, (i-5) * 30 + 20, 120, 20);
            [btn setTitle:result[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            //            [btn addTarget:self action:@selector(hotWorldClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn addTarget:self action:@selector(hotWorldClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.rightView addSubview:btn];
        }
    }
    [UIView animateWithDuration:0.8 animations:^{
        self.leftView.left = 0;
        self.rightView.left = 140;
    }];
}

- (void)hotWorldClick:(UIButton *)btn
{
    [_searchBar becomeFirstResponder];
    _searchBar.text = btn.titleLabel.text;
    _ddList._searchText = _searchBar.text;
    [_ddList updateData];
    [self setDDListHidden:NO];
}



- (void)setDDListHidden:(BOOL)hidden {
	NSInteger height = hidden ? 0 : kScreenHeight-45-44-20;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:.2];
	[_ddList.view setFrame:CGRectMake(0, 45, kScreenWidth, height)];
	[UIView commitAnimations];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark PassValue protocol
- (void)passValue:(NSDictionary *)value{
	if (value) {
		_searchBar.text = [value objectForKey:@"title"];
		[self searchBarSearchButtonClicked:_searchBar];
        GiftDetailViewController *giftDetailVC = [[GiftDetailViewController alloc] init];
        giftDetailVC.detailID = [value objectForKey:@"id"];
        [self.navigationController pushViewController:giftDetailVC animated:YES];
	}
	else {
	}
}

#pragma mark -
#pragma mark SearchBar Delegate Methods
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
	if ([searchText length] != 0) {
		_ddList._searchText = searchText;
		[_ddList updateData];
		[self setDDListHidden:NO];
	}
	else {
		[self setDDListHidden:YES];
	}
    
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
	searchBar.showsCancelButton = YES;
	for(id cc in [searchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
	return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
	searchBar.text = @"";
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
	searchBar.showsCancelButton = NO;
//	searchBar.text = @"";
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
	[self setDDListHidden:NO];
	[searchBar resignFirstResponder];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
	[self setDDListHidden:YES];
	[searchBar resignFirstResponder];
}

@end
